package com.ctsi.ssdc.controller;

import com.ctsi.ssdc.entity.Excel14;
import com.ctsi.ssdc.entity.SocialRetailSales;
import com.ctsi.ssdc.entity.TradeAndServiceIndustry;
import com.ctsi.ssdc.service.Excel14Service;
import com.ctsi.ssdc.service.ExcelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 14基础表服务业分行业(Excel14)表控制层
 *
 * @author makejava
 * @since 2023-02-21 18:54:37
 */
@RestController
@RequestMapping("excel/14")
public class Excel14Controller {
    /**
     * 服务对象
     */
    @Resource
    private Excel14Service excel14Service;
    @Resource
    private ExcelService excelService;

    /**
     * 分页查询
     *
     * @param excel14 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<Excel14>> queryByPage(Excel14 excel14,@PageableDefault Pageable pageRequest) {
        return ResponseEntity.ok(this.excel14Service.queryByPage(excel14,(PageRequest) pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Excel14> queryById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(this.excel14Service.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param excel14 实体
     * @return 新增结果
     */
    @PostMapping
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<Excel14> add(Excel14 excel14) {
        Excel14 insert = this.excel14Service.insert(excel14);
        excelService.sheet14Cal(excel14.getStatisticalTime());
        return ResponseEntity.ok(insert);
    }

    /**
     * 编辑数据
     *
     * @param excel14 实体
     * @return 编辑结果
     */
    @PutMapping
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<Excel14> edit(Excel14 excel14) {
        Excel14 update = this.excel14Service.update(excel14);
        excelService.sheet14Cal(excel14.getStatisticalTime());
        return ResponseEntity.ok(update);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<Boolean> deleteById(Long id) {
        Excel14 excel14 = excel14Service.queryById(id);
        boolean del = this.excel14Service.deleteById(id);
        excelService.sheet14Cal(excel14.getStatisticalTime());
        return ResponseEntity.ok(del);
    }

    /**
     * 交通及营利性服务业营业额当月 + 累计 + 同比:（本期累计/同期累计-1）*100%
     * @param year
     * @param month
     * @return
     */
    @GetMapping("trafficTurnoverTotal/{year}/{month}")
    @ApiOperation("交通及营利性服务业营业额当月 + 累计 + 同比:（本期累计/同期累计-1）*100%")
    public ResponseEntity<TradeAndServiceIndustry> getTrafficTurnoverTotal(@PathVariable("year") String year,
                                                                           @PathVariable("month") String month) {
        return ResponseEntity.ok(excel14Service.getTrafficTurnoverTotal(year,month));
    }

    /**
     * 批量修改值 (千元) -> (万元)
     * 需求 -> {
     *     22年里14页上服务业基础表单位是千元，改成亿元不要换算错了，其余基础表里都是万元。23年我都改成万元。
     * }
     */
    @PutMapping("changeValue/{year}")
    @ApiOperation("批量修改交通及营利性服务业营业额当月 + 累计")
    public Boolean changeValue(@PathVariable String year) {
        return excel14Service.changeValue(year);
    }

    /**
     * 批量修改值 (千元) -> (万元)
     * 需求 -> {
     *     22年里14页上服务业基础表单位是千元，改成亿元不要换算错了，其余基础表里都是万元。23年我都改成万元。
     * }
     */
    @PutMapping("changeValuePre/{year}")
    @ApiOperation("批量修改交通及营利性服务业营业额同期当月 + 同期累计")
    public Boolean changeValuePre(@PathVariable String year) {
        return excel14Service.changeValuePre(year);
    }

}

